# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 随机生成100万条数据.py
# @Author: dongguangwen
# @Date  : 2025-05-25 17:45
import random
import json
from datetime import datetime, timedelta
from faker import Faker

fake = Faker(["zh_CN"])
Faker.seed(0)


def random_date_time():
    # 设置一个时间范围，比如从 2020-01-01 到 2025-12-31
    start = datetime.strptime('2020-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
    end = datetime.strptime('2025-12-31 23:59:59', '%Y-%m-%d %H:%M:%S')

    # 计算时间差
    delta = end - start
    random_second = random.randint(0, int(delta.total_seconds()))

    # 得到随机时间
    random_time = start + timedelta(seconds=random_second)

    # 输出格式化时间字符串
    return random_time.strftime('%Y-%m-%d %H:%M:%S')


def get_data():
    # 个人基本信息
    name = fake.name()  # 生成姓名
    sex = random.choice(["male", "female"])  # 生成性别
    age = random.randint(1, 100)  # 生成年龄
    email = fake.email()  # 邮箱
    simple_profile = fake.simple_profile()  # 生成简单个人信息
    simple_profile['birthdate'] = simple_profile['birthdate'].strftime('%Y-%m-%d')
    profile = fake.profile()  # 生成更多个人信息
    profile['birthdate'] = profile['birthdate'].strftime('%Y-%m-%d')
    profile.pop('current_location')
    time = random_date_time()  # 时间
    info_person_key = ["name", "sex", "age", "email", "simple_profile", "profile", "time"]
    info_person_value = [name, sex, age, email, simple_profile, profile, time]

    # 地址类
    country = fake.country()  # 生成国家
    province = fake.province()  # 生成省份
    city = fake.city()  # 生成城市
    address = fake.address()  # 生成详细地址
    street_address = fake.street_address()  # 生成街道
    street_name = fake.street_name()  # 生成街道名
    postcode = fake.postcode()  # 生成邮编
    longitude = fake.longitude()  # 生成经度
    latitude = fake.latitude()  # 生成维度
    gps = [float(longitude), float(latitude)]
    info_address_key = ["country", "province", "city", "address", "street_address", "street_name", "postcode", "gps"]
    info_address_value = [country, province, city, address, street_address, street_name, postcode, gps]

    # 互联网类型
    ipv4 = fake.ipv4(network=False)  # ipv4地址
    ipv6 = fake.ipv6(network=False)  # ipv6地址
    uri = fake.uri()  # uri
    url = fake.url()  # url
    img_url = fake.image_url(width=None, height=None)  # 图片url
    domain = fake.domain_name()  # 域名
    user_agent = fake.user_agent()  # UA
    info_net_key = ["ipv4", "ipv6", "uri", "url", "img_url", "domain", "user_agent"]
    info_net_value = [ipv4, ipv6, uri, url, img_url, domain, user_agent]

    # 文本
    character = ''.join([fake.paragraph(nb_sentences=2, variable_nb_sentences=True) for _ in range(8)])
    long_text = ''.join([fake.paragraph(nb_sentences=3, variable_nb_sentences=True) for _ in range(9)])
    info_text_key = ["character", "long_text"]
    info_text_value = [character, long_text]

    info_key, info_value = [], []
    info_key.extend(info_person_key)
    info_key.extend(info_address_key)
    info_key.extend(info_net_key)
    info_key.extend(info_text_key)

    info_value.extend(info_person_value)
    info_value.extend(info_address_value)
    info_value.extend(info_net_value)
    info_value.extend(info_text_value)
    person_info = dict(zip(info_key, info_value))
    return person_info


with open('./person_info_100w.json', 'w', encoding='utf-8') as f:
    # for i in range(2):
    for i in range(1000000):
        person_infos = get_data()
        # print(person_infos)
        person_infos.update({'id': i + 1})
        f.write(json.dumps(person_infos, ensure_ascii=False) + '\n')
        if i % 1000 == 0:
            print(i)
